﻿using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Mod_InitSet_frm_CustomersInfo : System.Web.UI.Page
{
    public void ChangTab(object sender, ImageClickEventArgs e)
    {
        ImageButton lbtn = (ImageButton)sender;
        int intCommand = Int32.Parse(lbtn.CommandName);
        int intIndex = Int32.Parse(lbtn.CommandArgument);

        ImageButton lbtn1 = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtn1");
        ImageButton lbtn2 = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtn2");
        ImageButton lbtn3 = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtn3");
        ImageButton lbtn4 = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtn4");
        ImageButton lbtn5 = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtn5");
        lbtn1.ImageUrl = "../App_Images/b_50_2.gif";
        lbtn2.ImageUrl = "../App_Images/b_50_4.gif";
        lbtn3.ImageUrl = "../App_Images/b_50_6.gif";
        lbtn4.ImageUrl = "../App_Images/b_50_8.gif";
        lbtn5.ImageUrl = "../App_Images/b_50_10.gif";
        
        try
        {
            //设置从表新增刷新和作废
            this.ibtnAdd.CommandArgument = intCommand.ToString();
            this.ibtnRef.CommandArgument = intCommand.ToString();
            this.ibtnDelete0.CommandArgument = intCommand.ToString();
            this.lbtnDelete0.CommandArgument = intCommand.ToString();
            this.lbtnDelete1.CommandArgument = intCommand.ToString();
            int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
            bool is_Audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[4].Text.Trim());

            CustomerAddress customerAddress = (CustomerAddress)this.dgrdResult.Items[intIndex].FindControl("CustomerAddress");
            CustomerOffice customerOffice = (CustomerOffice)this.dgrdResult.Items[intIndex].FindControl("CustomerOffice");
            CustomerLicence customerLicence = (CustomerLicence)this.dgrdResult.Items[intIndex].FindControl("CustomerLicence");
            CustomerCharter customerCharter = (CustomerCharter)this.dgrdResult.Items[intIndex].FindControl("CustomerCharter");
            CustomerPrice customerPrice = (CustomerPrice)this.dgrdResult.Items[intIndex].FindControl("CustomerPrice");
            customerAddress.Visible = customerOffice.Visible = customerLicence.Visible = customerCharter.Visible = customerPrice.Visible = false;

            if (intCommand == 1)
            {
                lbtn1.ImageUrl = "../App_Images/b_50_1.gif";
                customerAddress.Visible = true;
                customerAddress.CustomerID = customerID;
                customerAddress.IsAudit = is_Audit;
            }
            else if (intCommand == 2)
            {
                lbtn2.ImageUrl = "../App_Images/b_50_3.gif";
                customerOffice.Visible = true;
                customerOffice.CustomerID = customerID;
                customerOffice.IsAudit = is_Audit;
            }
            else if (intCommand == 3)
            {
                lbtn3.ImageUrl = "../App_Images/b_50_5.gif";
                customerLicence.Visible = true;
                customerLicence.CustomerID = customerID;
                customerLicence.IsAudit = is_Audit;
            }
            else if (intCommand == 4)
            {
                lbtn4.ImageUrl = "../App_Images/b_50_7.gif";
                customerCharter.Visible = true;
                customerCharter.CustomerID = customerID;
                customerCharter.IsAudit = is_Audit;
            }
            else
            {
                lbtn5.ImageUrl = "../App_Images/b_50_9.gif";
                customerPrice.Visible = true;
                customerPrice.CustomerID = customerID;
                customerPrice.IsAudit = is_Audit;
            }
        }
        catch{}
    }

    public void ChangPage(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strKey = ((LinkButton)sender).CommandName;
            int intPageIndex = this.dgrdResult.CurrentPageIndex;
            int intPageCounts = this.dgrdResult.PageCount;

            if (strKey == "0")
            {
                intPageIndex = 0;
            }
            else if (strKey == "1")
            {
                if (intPageIndex > 0) intPageIndex -= 1;
            }
            else if (strKey == "2")
            {
                if (intPageIndex < intPageCounts - 1) intPageIndex += 1;
            }
            else
            {
                intPageIndex = intPageCounts - 1;
            }

            this.BindCustomersInfoByDataView(dst, intPageIndex);
        }
    }

    public void AuditCustomerInfo(object sender, ImageClickEventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();
            ImageButton ibtn = (ImageButton)sender;

            int intCommand = Int32.Parse(ibtn.CommandName);

            if (intCommand == 0)//确认
            {
                int intCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intCount; i++)
                {
                    bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                    if (IsChecked)
                    {
                        bool is_audit = bool.Parse(this.dgrdResult.Items[i].Cells[4].Text.Trim());

                        if (!is_audit)
                        {
                            int intID = Int32.Parse(this.dgrdResult.Items[i].Cells[1].Text.Trim());
                            Boolean bolIsSucceed = cls_Customer.UpdateCustomersState(intID, 0, strUserID);

                            if (!bolIsSucceed)
                            {
                                this.lblJs.Text = "<script>alert('确认客户信息失败，请重试！')</script>";
                            }
                        }
                    }
                    else
                    { 
                        this.lblJs.Text = "<script>alert('此记录已确认！')</script>";
                    }
                }

                this.GetCustomersInfoByMain();
            }
            else//取消确认
            {
                int intIndex = Int32.Parse(ibtn.CommandArgument);
                int intID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());

                Boolean bolIsSucceed = cls_Customer.UpdateCustomersState(intID, 1, strUserID);

                if (bolIsSucceed)
                {
                    this.GetCustomersInfoByMain();
                }
                else
                {
                    this.lblJs.Text = "<script>alert('取消确认失败，请重试！')</script>";
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void AddCustomerInfo(object sender, ImageClickEventArgs e)
    { 
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();

            int intCommand = Int32.Parse(((ImageButton)sender).CommandName);

            //添加主表信息相关
            if (intCommand == 0)
            {
                //打开添加
                this.panAddResult.Visible = true;
                this.txt15.Text = strUserID;
                this.txt16.Text = DateTime.Now.ToString();
                this.lblJs.Text = "<script>document.getElementById('"+ this.txt1.ClientID +"').focus();</script>";

                //获取确认过的银行信息
                DataSet dstBank = (DataSet)Session["BankDataSet"];

                if (dstBank != null && dstBank.Tables[0].Rows.Count > 0)
                {
                    this.drop2.DataTextField = "bk_name";
                    this.drop2.DataValueField = "bk_id";
                    this.drop2.DataSource = dstBank;
                    this.drop2.DataBind();
                }
                else
                {
                    this.lblJs.Text = "<script>alert('获取开户行信息为空！')</script>";
                }
            }
            else if (intCommand == 1)
            {
                //取消添加
                this.panAddResult.Visible = false;
            }
            else if (intCommand == 2)
            {
                //保存添加
                string strNo = this.txt1.Text.Trim();
                int intExistCount = cls_Customer.CheckUpCustomersID(strNo);

                if (intExistCount == 0)
                {
                    string no = this.txt1.Text.Trim();				            //客户编码
	                string name = this.txt2.Text.Trim();			            //客户简称
	                string desc = this.txt3.Text.Trim();			            //客户全称
	                string tel = this.txt4.Text.Trim();				            //联系电话
	                string fax = this.txt5.Text.Trim();				            //传真号码
	                string addr = this.txt6.Text.Trim();			            //地址
	                string postno = this.txt7.Text.Trim();			            //邮编
	                string corporation = this.txt8.Text.Trim();		            //公司法人
	                string linkman = this.txt9.Text.Trim();			            //联系人
	                string email = this.txt10.Text.Trim();			            //EMAIL
	                string tel1 = this.txt11.Text.Trim();			            //联系电话
	                string fax1 = this.txt12.Text.Trim();			            //传真
	                string dept = this.ctxt1.Text.Trim();			            //所属部门
	                string principal = this.ctxt2.Text.Trim();		            //负责人
                    int custype = Int32.Parse(this.drop1.SelectedItem.Value);	//客户类型
                    string cussort = this.ctxt3.Text.Trim();					//客户类别
                    string bank = this.drop2.SelectedItem.Value;				//开户行
                    string accounts = this.txt13.Text.Trim();			        //帐号
                    string revenue = this.txt14.Text.Trim();		        	//税号
                    string remark = this.txt19.Text.Trim();		                //备注

                    //非空验证
                    ArrayList txtBoxArray = new ArrayList();
                    txtBoxArray.Add(this.txt1);
                    txtBoxArray.Add(this.txt2);
                    txtBoxArray.Add(this.txt3);
                    txtBoxArray.Add(this.txt6);
                    txtBoxArray.Add(this.txt7);

                    ClsSystemSetDAL cls_System = new ClsSystemSetDAL();
                    string strError = cls_System.FieldValidator(txtBoxArray);

                    if (strError == "")
                    {
                        if (dept != "")
                        {
                            if (principal != "")
                            {
                                Boolean is_Succeed = cls_Customer.AddOrUpdateCustomersInfo(-1, no, name, desc, tel, fax, addr, postno, corporation, linkman, email, tel1, fax1, dept, principal, custype, cussort, bank, accounts, revenue, remark, strUserID);

                                if (is_Succeed)
                                {
                                    this.GetCustomersInfoByMain();

                                    this.txt1.Text = this.txt2.Text = this.txt3.Text = this.txt4.Text = this.txt5.Text = this.txt6.Text = this.txt7.Text = this.txt8.Text = this.txt9.Text = this.txt10.Text = this.txt11.Text = this.txt12.Text = this.ctxt1.Text = this.ctxt2.Text = this.ctxt3.Text = this.txt13.Text = this.txt14.Text = this.txt19.Text = "";
                                }
                                else
                                {
                                    this.lblJs.Text = "<script>alert('新增客户基本信息失败，请重试！')</script>";
                                }
                            }
                            else
                            {
                                this.lblJs.Text = "<script>alert('负责人不能为空！')</script>"; 
                            }
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('所属部门不能为空！')</script>";
                        }
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('" + strError + "')</script>";
                    }
                }
                else if (intExistCount > 0)
                {
                    this.lblJs.Text = "<script>alert('此客户编号已存在，请用其它编号！')</script>";
                }
                else
                {
                    this.lblJs.Text = "<script>alert('检验客户编号失败，请重试！')</script>";
                }
            }

            //添加从表信息相关
            else
            {
                int intIndex = Int32.Parse(this.dgrdResult.ToolTip);

                try
                {
                    intCommand = Int32.Parse(((ImageButton)sender).CommandArgument);
                    int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
                    bool is_Audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[4].Text.Trim());

                    if (intCommand == 1)
                    {
                        //收货地址
                        CustomerAddress customerAddress = (CustomerAddress)this.dgrdResult.Items[intIndex].FindControl("CustomerAddress");
                        customerAddress.Command = 0;
                        customerAddress.CustomerID = customerID;
                        customerAddress.IsAudit = is_Audit;
                    }
                    else if (intCommand == 2)
                    {
                        //科室信息
                        CustomerOffice customerOffice = (CustomerOffice)this.dgrdResult.Items[intIndex].FindControl("CustomerOffice");
                        customerOffice.Command = 0;
                        customerOffice.CustomerID = customerID;
                        customerOffice.IsAudit = is_Audit;
                    }
                    else if (intCommand == 3)
                    {
                        //许可证信息
                        CustomerLicence customerLicence = (CustomerLicence)this.dgrdResult.Items[intIndex].FindControl("CustomerLicence");
                        customerLicence.Command = 0;
                        customerLicence.CustomerID = customerID;
                        customerLicence.IsAudit = is_Audit;
                    }
                    else if (intCommand == 4)
                    {
                        //营业执照信息
                        CustomerCharter customerCharter = (CustomerCharter)this.dgrdResult.Items[intIndex].FindControl("CustomerCharter");
                        customerCharter.Command = 0;
                        customerCharter.CustomerID = customerID;
                        customerCharter.IsAudit = is_Audit;
                    }
                    else
                    {
                        //客户价格
                        CustomerPrice customerPrice = (CustomerPrice)this.dgrdResult.Items[intIndex].FindControl("CustomerPrice");
                        customerPrice.Command = 0;
                        customerPrice.CustomerID = customerID;
                        customerPrice.IsAudit = is_Audit;
                    }
                }
                catch { }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void UpdateCustomerInfo(object sender, ImageClickEventArgs e)
    { 
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            ImageButton ibtn = (ImageButton)sender;

            int intCommand = Int32.Parse(ibtn.CommandName);
            
            if (intCommand == 1)//取消编辑
            { 
                this.GetCustomersInfoByMain();
            }
            else  
            {
                ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();
                int intIndex = Int32.Parse(ibtn.CommandArgument);

                CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
                ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
                ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
                ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");

                DropDownList dropRs1 = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("dropRs1");
                DropDownList dropRs2 = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("dropRs2");
                CuTextSelectBox ctxtRs1 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctxtRs1");
                CuTextSelectBox ctxtRs2 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctxtRs2");
                CuTextSelectBox ctxtRs3 = (CuTextSelectBox)this.dgrdResult.Items[intIndex].FindControl("ctxtRs3");
                TextBox dtxtRs1 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("dtxtRs1");
                TextBox dtxtRs2 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("dtxtRs2");
                TextBox txtRs1 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs1");
                TextBox txtRs2 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs2");
                TextBox txtRs3 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs3");
                TextBox txtRs4 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs4");
                TextBox txtRs5 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs5");
                TextBox txtRs6 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs6");
                TextBox txtRs7 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs7");
                TextBox txtRs8 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs8");
                TextBox txtRs9 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs9");
                TextBox txtRs10 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs10");
                TextBox txtRs11 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs11");
                TextBox txtRs12 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs12");
                TextBox txtRs13 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs13");
                TextBox txtRs14 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs14");
                TextBox txtRs19 = (TextBox)this.dgrdResult.Items[intIndex].FindControl("txtRs19");

                if (intCommand == 0)//打开编辑 
                {
                    chkRsSelect.Visible = ibtnRsEdit.Visible = false;
                    ibtnRsCancel.Visible = ibtnRsServer.Visible = true;
                    dtxtRs1.Visible = dtxtRs2.Visible = false;
                    dropRs1.Visible = dropRs2.Visible = true;
                    ctxtRs1.ReadOnly = ctxtRs2.ReadOnly = ctxtRs3.ReadOnly = false;
                    txtRs2.ReadOnly = txtRs3.ReadOnly = txtRs4.ReadOnly = txtRs5.ReadOnly = false;
                    txtRs6.ReadOnly = txtRs7.ReadOnly = txtRs8.ReadOnly = txtRs9.ReadOnly = txtRs10.ReadOnly = false;
                    txtRs11.ReadOnly = txtRs12.ReadOnly = txtRs13.ReadOnly = txtRs14.ReadOnly = txtRs19.ReadOnly = false;
                    txtRs2.BorderWidth = txtRs3.BorderWidth = txtRs4.BorderWidth = txtRs5.BorderWidth = 1;
                    txtRs6.BorderWidth = txtRs7.BorderWidth = txtRs8.BorderWidth = txtRs9.BorderWidth = txtRs10.BorderWidth = 1;
                    txtRs11.BorderWidth = txtRs12.BorderWidth = txtRs13.BorderWidth = txtRs14.BorderWidth = txtRs19.BorderWidth = 1;

                    ctxtRs1.Text = ctxtRs1.ToolTip;
                    ctxtRs2.Text = ctxtRs2.ToolTip;
                    ctxtRs3.Text = ctxtRs3.ToolTip;

                    this.lblJs.Text = "<script>document.getElementById('" + txtRs2.ClientID + "').select();document.getElementById('" + txtRs1.ClientID + "').focus();</script>";
                }
                else//保存编辑
                {   
                    int id = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());     //ID号
                    string no = txtRs1.Text.Trim();				                                    //客户编码
                    string name = txtRs2.Text.Trim();			                                    //客户简称
                    string desc = txtRs3.Text.Trim();			                                    //客户全称
                    string tel = txtRs4.Text.Trim();				                                //联系电话
                    string fax = txtRs5.Text.Trim();				                                //传真号码
                    string addr = txtRs6.Text.Trim();			                                    //地址
                    string postno = txtRs7.Text.Trim();			                                    //邮编
                    string corporation = txtRs8.Text.Trim();		                                //公司法人
                    string linkman = txtRs9.Text.Trim();			                                //联系人
                    string email = txtRs10.Text.Trim();			                                    //EMAIL
                    string tel1 = txtRs11.Text.Trim();			                                    //联系电话
                    string fax1 = txtRs12.Text.Trim();			                                    //传真
                    string dept = ctxtRs1.Text.Trim();			                                    //所属部门
                    string principal = ctxtRs2.Text.Trim();		                                    //负责人
                    int custype = Int32.Parse(dropRs1.SelectedItem.Value);	                        //客户类型
                    string cussort = ctxtRs3.Text.Trim();					                        //客户类别
                    string bank = dropRs2.SelectedItem.Value;				                        //开户行
                    string accounts = txtRs13.Text.Trim();			                                //帐号
                    string revenue = txtRs14.Text.Trim();		        	                        //税号
                    string remark = txtRs19.Text.Trim();		                                    //备注

                    //非空验证
                    ArrayList txtBoxArray = new ArrayList();
                    txtBoxArray.Add(txtRs2);
                    txtBoxArray.Add(txtRs3);
                    txtBoxArray.Add(txtRs6);
                    txtBoxArray.Add(txtRs7);

                    ClsSystemSetDAL cls_System = new ClsSystemSetDAL();
                    string strError = cls_System.FieldValidator(txtBoxArray);

                    if (strError == "")
                    {
                        if (dept != "")
                        {
                            if (principal != "")
                            {
                                Boolean is_Succeed = cls_Customer.AddOrUpdateCustomersInfo(id, no, name, desc, tel, fax, addr, postno, corporation, linkman, email, tel1, fax1, dept, principal, custype, cussort, bank, accounts, revenue, remark, strUserID);

                                if (is_Succeed)
                                {
                                    this.GetCustomersInfoByMain();
                                }
                                else
                                {
                                    this.lblJs.Text = "<script>alert('修改客户基本信息失败，请重试！')</script>";
                                }
                            }
                            else
                            {
                                this.lblJs.Text = "<script>alert('负责人不能为空！')</script>";
                            }
                        }
                        else
                        {
                            this.lblJs.Text = "<script>alert('所属部门不能为空！')</script>";
                        }
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('" + strError + "')</script>";
                    }
                }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    public void OpenChildResult(object sender, ImageClickEventArgs e)
    {
        //获取当前的ImageButton控件
        ImageButton ibtnOpen = (ImageButton)sender;

        //获取要操作当前项的类型(0展开，1收缩)和当前项再DataGrid中的索引位置
        int intCommandName = Int32.Parse(ibtnOpen.CommandName);
        int intIndex = Int32.Parse(ibtnOpen.CommandArgument);

        //获取当前项中要控制的控件
        CheckBox chkRsSelect = (CheckBox)this.dgrdResult.Items[intIndex].FindControl("chkRsSelect");
        ImageButton ibtnOpenChild = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnOpenChild");
        ImageButton ibtnRsCancel = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsCancel");
        ImageButton ibtnRsEdit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsEdit");
        ImageButton ibtnRsServer = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsServer");
        ImageButton ibtnRsOAudit = (ImageButton)this.dgrdResult.Items[intIndex].FindControl("ibtnRsOAudit");
        Panel panShowChildResult = (Panel)this.dgrdResult.Items[intIndex].FindControl("panShowChildResult");

        //展开当前项中该单据主从表信息控制相关
        if (intCommandName == 0)
        {
            //用循环找到当前展开的项将其展开
            int intCount = this.dgrdResult.Items.Count;

            for (int i = 0; i < intCount; i++)
            {
                Panel panItem = (Panel)this.dgrdResult.Items[i].FindControl("panItem");

                panItem.Visible = (i == intIndex) ? true : false;
            }

            //主表展开后则主表不可操作控制
            this.dgrdResult.ToolTip = intIndex.ToString();
            this.ibtnAdd.CommandName = "3";
            this.ibtnAdd.CommandArgument = "1";
            this.ibtnRef.CommandName = "1";
            this.ibtnRef.CommandArgument = "1";
            this.ibtnDelete0.CommandArgument = "1";
            this.lbtnDelete0.CommandArgument = "1";
            this.lbtnDelete1.CommandArgument = "1";
            ibtnOpenChild.CommandName = "1";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/10.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/16.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/18.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_3.gif";
            panShowChildResult.Visible = true;
            this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;

            //获取当前项中该项单据的状态(是否已确认)
            int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
            bool is_audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[4].Text.Trim());

            //如果主表确认或终止后则从表不可操作(添加和作废)
            if (is_audit)
            {
                this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = false;
                this.ibtnAdd.ImageUrl = "../App_Images/b_18_3.gif";
                this.ibtnDelete0.ImageUrl = "../App_Images/b_19_5.gif";
                this.imgDelete.ImageUrl = "../App_Images/b_19_6.gif";                       
                this.imgDelete.Attributes.Add("OnMouseOver", "");
                this.imgDelete.Attributes.Add("OnMouseOut", "");
                this.imgDelete.Attributes.Add("OnClick", "");
            }

            //显示收货地址从表用户控件
            CustomerAddress customerAddress = (CustomerAddress)this.dgrdResult.Items[intIndex].FindControl("CustomerAddress");
            customerAddress.Visible = true;
            customerAddress.CustomerID = customerID;
            customerAddress.IsAudit = is_audit;

            //当客户客户类型位医院时才有科室信息
            Panel panIsVisibleTab2 = (Panel)this.dgrdResult.Items[intIndex].FindControl("panIsVisibleTab2");
            DropDownList dropRs1 = (DropDownList)this.dgrdResult.Items[intIndex].FindControl("dropRs1");

            if (dropRs1.SelectedIndex == 0)
            {
                panIsVisibleTab2.Visible = true;
            }
            else
            {
                panIsVisibleTab2.Visible = false;
            }
        }
        else//收缩当前项中该单据主表信息控制相关
        {
            this.ibtnAdd.CommandName = "0";
            this.ibtnRef.CommandName = "0";
            ibtnOpenChild.CommandName = "0";
            ibtnOpenChild.ImageUrl = "../App_Images/ico/9.gif";
            ibtnRsEdit.ImageUrl = "../App_Images/ico/12.gif";
            ibtnRsServer.ImageUrl = "../App_Images/ico/11.gif";
            ibtnRsOAudit.ImageUrl = "../App_Images/ico/8.gif";
            ibtnRsCancel.ImageUrl = "../App_Images/ico/4.gif";
            this.ibtnAudit.ImageUrl = "../App_Images/b_21_1.gif";
            this.ibtnAdd.ImageUrl = "../App_Images/b_18_1.gif";
            this.ibtnDelete0.ImageUrl = "../App_Images/b_19_1.gif";
            this.imgDelete.ImageUrl = "../App_Images/b_19_4.gif";
            this.imgDelete.Attributes.Add("OnMouseOver", "ChangImage(this, '../App_Images/b_19_3.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_2.gif')");
            this.imgDelete.Attributes.Add("OnMouseOut", "ChangImage(this, '../App_Images/b_19_4.gif');ChangImage(document.all.ibtnDelete0, '../App_Images/b_19_1.gif')");
            this.imgDelete.Attributes.Add("OnClick", "ChildMenuVisible()");
            panShowChildResult.Visible = false;
            this.ibtnAdd.Enabled = this.ibtnDelete0.Enabled = this.ibtnAudit.Enabled = chkRsSelect.Enabled = ibtnRsCancel.Enabled = ibtnRsEdit.Enabled = ibtnRsServer.Enabled = ibtnRsOAudit.Enabled = true;

            //重新绑定主表数据
            this.GetCustomersInfoByMain();
        }
    }

    public void lbtnDelete_Click(object sender, EventArgs e)
    {
        int intCommandName = Int32.Parse(((LinkButton)sender).CommandName);

        this.DeleteCustomerInfo(intCommandName);
    }

    private void DeleteCustomerInfo(int CommandName)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            int intCommand = Int32.Parse(this.ibtnRef.CommandName);

            if (intCommand == 0)//主表作废
            {
                ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();
                int intCount = this.dgrdResult.Items.Count;

                for (int i = 0; i < intCount; i++)
                {
                    bool IsChecked = ((CheckBox)this.dgrdResult.Items[i].FindControl("chkRsSelect")).Checked;

                    if (IsChecked)
                    {
                        bool is_audit = bool.Parse(this.dgrdResult.Items[i].Cells[4].Text.Trim());

                        if (!is_audit)
                        {
                            Boolean bolIsSucceed = false;
                            int intID = Int32.Parse(this.dgrdResult.Items[i].Cells[1].Text.Trim());

                            if (CommandName == 1)//不彻底作废
                            {
                                bolIsSucceed = cls_Customer.UpdateCustomersState(intID, 2, strUserID);
                            }
                            else//彻底作废
                            {
                                bolIsSucceed = cls_Customer.UpdateCustomersState(intID, 3, strUserID);
                            }

                            if (!bolIsSucceed)
                            {
                                this.lblJs.Text = "<script>alert('作废记录失败，请重试！')</script>";
                            }
                        }
                        else
                        { 
                            this.lblJs.Text = "<script>alert('此记录已确认，不能作废！')</script>";
                        }
                    }
                }

                //重新绑定主表结果
                this.GetCustomersInfoByMain();
            }
            else//从表作废
            {
                intCommand = Int32.Parse(this.ibtnRef.CommandArgument);
                int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
                int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());

                try
                {
                    if (intCommand == 1)
                    {
                        //收货地址
                        CustomerAddress customerAddress = (CustomerAddress)this.dgrdResult.Items[intIndex].FindControl("CustomerAddress");
                        customerAddress.CustomerID = customerID;
                        customerAddress.Command = (CommandName == 1) ? 1 : 2;
                    }
                    else if (intCommand == 2)
                    {
                        //科室信息
                        CustomerOffice customerOffice = (CustomerOffice)this.dgrdResult.Items[intIndex].FindControl("CustomerOffice");
                        customerOffice.CustomerID = customerID;
                        customerOffice.Command = (CommandName == 1) ? 1 : 2;
                    }
                    else if (intCommand == 3)
                    {
                        //许可证信息
                        CustomerLicence customerLicence = (CustomerLicence)this.dgrdResult.Items[intIndex].FindControl("CustomerLicence");
                        customerLicence.CustomerID = customerID;
                        customerLicence.Command = (CommandName == 1) ? 1 : 2;
                    }
                    else if (intCommand == 4)
                    {
                        //营业执照信息
                        CustomerCharter customerCharter = (CustomerCharter)this.dgrdResult.Items[intIndex].FindControl("CustomerCharter");
                        customerCharter.CustomerID = customerID;
                        customerCharter.Command = (CommandName == 1) ? 1 : 2;
                    }
                    else
                    {
                        //客户价格
                        CustomerPrice customerPrice = (CustomerPrice)this.dgrdResult.Items[intIndex].FindControl("CustomerPrice");
                        customerPrice.CustomerID = customerID;
                        customerPrice.Command = (CommandName == 1) ? 1 : 2;
                    }
                }
                catch { }
            }
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    private void GetCustomersInfoByMain()
    {
        ClsCustomersInfoDAL cls_Customers = new ClsCustomersInfoDAL();
        DataSet dst = cls_Customers.GetCustomersInfo(0);

        this.Page.Session["BankDataSet"] = cls_Customers.GetAuditBankInfo();

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            ClsPropertyDAL cls_Property = new ClsPropertyDAL();
            cls_Property.SetOrGetCorporationDataSet = dst;

            this.BindCustomersInfoByDataView(dst, 0);
        }
        else
        {
            this.dgrdResult.CurrentPageIndex = 0;
            this.dgrdResult.DataSource = null;
            this.dgrdResult.DataBind();
            this.lblRsCounts.Text = "共0条记录";
            this.lblPageCounts.Text = "共0页";
            this.lblCurrPage.Text = "当前第0页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = false;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = false;
        }
    }

    private void BindCustomersInfoByDataView(DataSet dst, int CurrPageIndex)
    {
        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            this.dgrdResult.CurrentPageIndex = CurrPageIndex;
            this.dgrdResult.DataSource = dst;
            this.dgrdResult.DataBind();

            this.lblRsCounts.Text = "共" + dst.Tables[0].Rows.Count + "条记录";
            this.lblPageCounts.Text = "共" + this.dgrdResult.PageCount + "页";
            this.lblCurrPage.Text = "当前第" + (this.dgrdResult.CurrentPageIndex + 1) + "页";
            this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = this.txtGo.Enabled = true;
            this.lblRsCounts.Enabled = this.lblPageCounts.Enabled = this.lblCurrPage.Enabled = this.lblGoTxt1.Enabled = this.lblGoTxt2.Enabled = true;

            if (this.dgrdResult.PageCount == 1)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
                this.lblGoTxt1.Enabled = this.txtGo.Enabled = this.lblGoTxt2.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == this.dgrdResult.PageCount - 1)
            {
                this.lbtnNext.Enabled = this.lbtnEnd.Enabled = false;
            }
            else if (this.dgrdResult.CurrentPageIndex == 0)
            {
                this.lbtnFirst.Enabled = this.lbtnPrev.Enabled = false;
            }
        }
    }

    private void ControlButtonVisible(string UserLogID, string CurrPageID)
    {
        ClsMenuDAL cls_Menu = new ClsMenuDAL();
        DataSet dst = cls_Menu.GetCurrUserButtonPopedom(UserLogID, CurrPageID);

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            int intCount = dst.Tables[0].Rows.Count - 1;

            for (int i = intCount; i >= 0; i--)
            {
                int intButtonID = Int32.Parse(dst.Tables[0].Rows[i][2].ToString());

                switch (intButtonID)
                {
                    case 0:
                        this.panAdd.Visible = true;
                        break;
                    case 1:
                        this.hidButtonEdit.Value = intButtonID.ToString();
                        break;
                    case 2:
                        this.panDelete.Visible = true;
                        break;
                    case 3:
                        this.lbtnDelete1.Enabled = true;
                        break;
                    case 4:
                        this.panAudit.Visible = true;
                        break;
                    case 5:
                        this.hidButtonOAudit.Value = intButtonID.ToString();
                        break;
                    case 6:
                        this.panRef.Visible = true;
                        break;
                    case 7:
                        this.panSearch.Visible = true;
                        break;
                    case 20:
                        this.panPrint.Visible = true;
                        break;
                }
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();

        string strUserID = cls_Property.SetOrGetUserID;

        if (strUserID != "")
        {
            if (!IsPostBack)
            {
                ////////////////按钮权限控制 Begin /////////////////////////
                string strCurrPageID = this.Page.Request.Params["PageID"];
                this.hidCurrPageID.Value = strCurrPageID;
                this.ControlButtonVisible(strUserID, strCurrPageID);
                //////////////////////  End ///////////////////////////////

                this.GetCustomersInfoByMain();
            }
            else
            {
                this.lblJs.Text = "";//清空Js脚本
            }

            this.ibtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete0.Attributes.Add("onClick", "return confirm('您真的要作废所选的项吗？');");
            this.lbtnDelete1.Attributes.Add("onClick", "return confirm('彻底作废将无法恢复数据。您真的要作废所选的项吗？');");
        }
        else
        {
            this.Page.Session.Remove("SetOrGetUserID");
            this.lblJs.Text = "<script>parent.window.location.href='../index.aspx'</script>";
        }
    }

    protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
    {
        string strKey = this.txtKey.Text.Trim();

        if (strKey != "" && strKey != "查询关键字")
        {
            ClsCustomersInfoDAL ClsCustomers = new ClsCustomersInfoDAL();
            DataSet dst = ClsCustomers.SearchCustomersInfo(strKey);

            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                ClsPropertyDAL cls_Property = new ClsPropertyDAL();
                cls_Property.SetOrGetCorporationDataSet = dst;

                this.BindCustomersInfoByDataView(dst, 0);
            }
            else
            {
                this.lblJs.Text = "<script>alert('搜索结果为空！')</script>";
            }
        }
        else
        {
            this.lblJs.Text = "<script>alert('请输入搜索关键字！')</script>";
        }
    }

    protected void txtGo_TextChanged(object sender, EventArgs e)
    {
        ClsPropertyDAL cls_Property = new ClsPropertyDAL();
        DataSet dst = cls_Property.SetOrGetCorporationDataSet;

        if (dst != null && dst.Tables[0].Rows.Count > 0)
        {
            string strIndex = this.txtGo.Text.Trim();
            this.txtGo.Text = String.Empty;

            if (strIndex != "")
            {
                try
                {
                    
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('请输入数字类型的页码！')</script>";
                }
            }
            else
            {
                this.lblJs.Text = "<script>alert('请输入要跳转的页码！')</script>";
            }
        }
    }

    protected void ibtnDelete0_Click(object sender, ImageClickEventArgs e)
    {
        int intCommandName = Int32.Parse(((ImageButton)sender).CommandName);

        this.DeleteCustomerInfo(intCommandName);
    }

    protected void ibtnRef_Click(object sender, ImageClickEventArgs e)
    {
        int intCommand = Int32.Parse(((ImageButton)sender).CommandName);

        if (intCommand == 0)//刷新主表
        {
            this.GetCustomersInfoByMain();
        }
        else//刷新从表
        {
            int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
            int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
            bool is_Audit = bool.Parse(this.dgrdResult.Items[intIndex].Cells[4].Text.Trim());

            try
            {
                intCommand = Int32.Parse(((ImageButton)sender).CommandArgument);

                if (intCommand == 1)
                {
                    //收货地址
                    CustomerAddress customerAddress = (CustomerAddress)this.dgrdResult.Items[intIndex].FindControl("CustomerAddress");
                    customerAddress.CustomerID = customerID;
                    customerAddress.IsAudit = is_Audit;
                }
                else if (intCommand == 2)
                {
                    //科室信息
                    CustomerOffice customerOffice = (CustomerOffice)this.dgrdResult.Items[intIndex].FindControl("CustomerOffice");
                    customerOffice.CustomerID = customerID;
                    customerOffice.IsAudit = is_Audit;
                }
                else if (intCommand == 3)
                {
                    //许可证信息
                    CustomerLicence customerLicence = (CustomerLicence)this.dgrdResult.Items[intIndex].FindControl("CustomerLicence");
                    customerLicence.CustomerID = customerID;
                    customerLicence.IsAudit = is_Audit;
                }
                else if (intCommand == 4)
                {
                    //营业执照信息
                    CustomerCharter customerCharter = (CustomerCharter)this.dgrdResult.Items[intIndex].FindControl("CustomerCharter");
                    customerCharter.CustomerID = customerID;
                    customerCharter.IsAudit = is_Audit;
                }
                else
                {
                    //客户价格
                    CustomerPrice customerPrice = (CustomerPrice)this.dgrdResult.Items[intIndex].FindControl("CustomerPrice");
                    customerPrice.CustomerID = customerID;
                    customerPrice.IsAudit = is_Audit;
                }
            }
            catch { }
        }
    }

    protected void dgrdResult_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            int custype = Int32.Parse(e.Item.Cells[2].Text.Trim());
            string bk_id = e.Item.Cells[3].Text.Trim();
            bool is_audit = bool.Parse(e.Item.Cells[4].Text.Trim());

            string strButtonEdit = this.hidButtonEdit.Value;
            string strButtonOAudit = this.hidButtonOAudit.Value;

            CheckBox chkRsSelect = (CheckBox)e.Item.FindControl("chkRsSelect");
            TextBox dtxtRs1 = (TextBox)e.Item.FindControl("dtxtRs1");
            TextBox dtxtRs2 = (TextBox)e.Item.FindControl("dtxtRs2");
            TextBox txtRs19 = (TextBox)e.Item.FindControl("txtRs19");
            DropDownList dropRs1 = (DropDownList)e.Item.FindControl("dropRs1");
            DropDownList dropRs2 = (DropDownList)e.Item.FindControl("dropRs2");
            ImageButton ibtnRsEdit = (ImageButton)e.Item.FindControl("ibtnRsEdit");
            ImageButton ibtnRsCancel = (ImageButton)e.Item.FindControl("ibtnRsCancel");
            ImageButton ibtnRsServer = (ImageButton)e.Item.FindControl("ibtnRsServer");
            ImageButton ibtnRsOAudit = (ImageButton)e.Item.FindControl("ibtnRsOAudit");

            //修改按回车提交
            if (txtRs19.ReadOnly)
            {
                txtRs19.Attributes.Add("onkeydown", "if(event.keyCode == 13){document.getElementById('" + ibtnRsServer.ClientID + "').click();}");
            }

            //判断主表是否已确认
            if (!is_audit)
            {
                chkRsSelect.Visible = true;
                ibtnRsEdit.Visible = true;

                //可编辑
                if (strButtonEdit.Trim() == "1")
                {
                    //不可选择
                    if (!this.panAudit.Visible && !this.panDelete.Visible)
                    {
                        chkRsSelect.Enabled = false;
                    }
                }
                else//不可编辑
                {
                    ibtnRsEdit.Enabled = false;
                    ibtnRsEdit.ImageUrl = "../App_Images/ico/14.gif";
                    ibtnRsEdit.ToolTip = "无编辑权限";

                    //不可选择
                    if (!this.panAudit.Visible && !this.panDelete.Visible)
                    {
                        chkRsSelect.Enabled = false;
                    }
                }
            }
            else
            {
                ibtnRsOAudit.Visible = true;

                if (strButtonOAudit.Trim() != "5")
                {
                    ibtnRsOAudit.Enabled = false;
                    ibtnRsOAudit.ImageUrl = "../App_Images/ico/17.gif";
                }
            }

            //获取确认过的银行信息
            DataSet dstBank = (DataSet)Session["BankDataSet"];

            if (dstBank != null && dstBank.Tables[0].Rows.Count > 0)
            {
                dropRs2.DataTextField = "bk_name";
                dropRs2.DataValueField = "bk_id";
                dropRs2.DataSource = dstBank;
                dropRs2.DataBind();

                for (int i = dropRs2.Items.Count - 1; i >= 0; i--)
                {
                    if (bk_id == dropRs2.Items[i].Value.Trim())
                    {
                        dropRs2.Items[i].Selected = true;
                        dtxtRs2.Text = dropRs2.Items[i].Text.Trim();
                        break;
                    }
                }
            }

            //客户类型
            dropRs1.SelectedIndex = custype;
            dtxtRs1.Text = dropRs1.SelectedItem.Text.Trim();
        }
    }

    protected void dgrdResult_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType.ToString() == "Item" || e.Item.ItemType.ToString() == "AlternatingItem")
        {
            //给用户控件注册事件
            CustomerAddress customerAddress = (CustomerAddress)e.Item.FindControl("CustomerAddress");
            customerAddress.AddClick += new AddressClickEventHandler(customerAddress_AddClick);
            customerAddress.CalcenClick += new AddressClickEventHandler(customerAddress_CalcenClick);
            customerAddress.UpdateClick += new AddressClickEventHandler(customerAddress_UpdateClick);
            CustomerOffice customerOffice = (CustomerOffice)e.Item.FindControl("CustomerOffice");
            customerOffice.AddClick += new OfficeClickEventHandler(customerOffice_AddClick);
            customerOffice.CalcenClick += new OfficeClickEventHandler(customerOffice_CalcenClick);
            customerOffice.UpdateClick += new OfficeClickEventHandler(customerOffice_UpdateClick);
            CustomerLicence customerLicence = (CustomerLicence)e.Item.FindControl("CustomerLicence");
            customerLicence.AddClick += new LicenceClickEventHandler(customerLicence_AddClick);
            customerLicence.CalcenClick += new LicenceClickEventHandler(customerLicence_CalcenClick);
            customerLicence.UpdateClick += new LicenceClickEventHandler(customerLicence_UpdateClick);
            CustomerCharter customerCharter = (CustomerCharter)e.Item.FindControl("CustomerCharter");
            customerCharter.AddClick += new CharterClickEventHandler(customerCharter_AddClick);
            customerCharter.CalcenClick += new CharterClickEventHandler(customerCharter_CalcenClick);
            customerCharter.UpdateClick += new CharterClickEventHandler(customerCharter_UpdateClick);
            CustomerPrice cusPrice = (CustomerPrice)e.Item.FindControl("CustomerPrice");
            cusPrice.AddClick += new PriceClickEventHandler(cusPrice_AddClick);
            cusPrice.CalcenClick += new PriceClickEventHandler(cusPrice_CalcenClick);
            cusPrice.UpdateClick += new PriceClickEventHandler(cusPrice_UpdateClick);
        }
    }

    protected void customerAddress_AddClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerAddress customerAddress = (CustomerAddress)this.dgrdResult.Items[intIndex].FindControl("CustomerAddress");

        int intCus_ID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        string strNo = customerAddress.No;
        string strName = customerAddress.Name;
        string strDesc = customerAddress.Desc;
        string strZip = customerAddress.Zip;
        bool is_Default = customerAddress.IsDefault;
        int intState = customerAddress.IsStop;

        customerAddress.CustomerID = intCus_ID;

        ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();

        int intExistCount = cls_Customer.CheckUpCustomerAddress(intCus_ID, strNo);

        if (intExistCount == 0)
        {
            if (strNo.Trim() != "" && strName.Trim() != "" && strDesc.Trim() != "" && strZip.Trim() != "")
            {
                Boolean is_Succeed = cls_Customer.AddOrUpdateCustomerAddress(-1, intCus_ID, strNo, strName, strDesc, strZip, is_Default, intState);

                if (is_Succeed)
                {
                    customerAddress.No = customerAddress.Name = customerAddress.Desc = customerAddress.Zip = "";
                    customerAddress.IsDefault = false;
                    customerAddress.IsStop = 0;
                }
                else
                {
                    this.lblJs.Text = "<script>alert('新增客户收货地址失败，请重试！');</script>";
                }
            }
        }
        else if (intExistCount > 0)
        {
            this.lblJs.Text = "<script>alert('当前客户该收货地址已存在！');</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('新增客户收货地址失败，请重试！');</script>";
        }
    }

    protected void customerAddress_CalcenClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerAddress customerAddress = (CustomerAddress)this.dgrdResult.Items[intIndex].FindControl("CustomerAddress");

        customerAddress.AddPanelVisible = false;
    }

    protected void customerAddress_UpdateClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        CustomerAddress customerAddress = (CustomerAddress)this.dgrdResult.Items[intIndex].FindControl("CustomerAddress");
        customerAddress.CustomerID = customerID;
        customerAddress.IsUpdate = true;
    }

    protected void customerOffice_AddClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerOffice customerOffice = (CustomerOffice)this.dgrdResult.Items[intIndex].FindControl("CustomerOffice");

        int intCus_ID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        string strNo = customerOffice.No;
        int intState = customerOffice.IsStop;

        customerOffice.CustomerID = intCus_ID;

        ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();

        int intExistCount = cls_Customer.CheckUpCustomerOffice(intCus_ID, strNo);

        if (intExistCount == 0)
        {
            if (strNo.Trim() != "")
            {
                Boolean is_Succeed = cls_Customer.AddOrUpdateCustomerOffice(-1, intCus_ID, strNo, intState);

                if (is_Succeed)
                {
                    customerOffice.No = "";
                    customerOffice.IsStop = 0;
                }
                else
                {
                    this.lblJs.Text = "<script>alert('新增客科室信息失败，请重试！');</script>";
                }
            }
        }
        else if (intExistCount > 0)
        {
            this.lblJs.Text = "<script>alert('当前客户该科室信息已存在！');</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('新增客户科室信息失败，请重试！');</script>";
        }
    }

    protected void customerOffice_CalcenClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerOffice customerOffice = (CustomerOffice)this.dgrdResult.Items[intIndex].FindControl("CustomerOffice");

        customerOffice.AddPanelVisible = false;
    }

    protected void customerOffice_UpdateClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        CustomerOffice customerOffice = (CustomerOffice)this.dgrdResult.Items[intIndex].FindControl("CustomerOffice");
        customerOffice.CustomerID = customerID;
        customerOffice.IsUpdate = true;
    }

    protected void customerLicence_AddClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerLicence customerLicence = (CustomerLicence)this.dgrdResult.Items[intIndex].FindControl("CustomerLicence");

        int intCus_ID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        string strNo = customerLicence.No;
        string strName = customerLicence.Name;
        string strCorpName = customerLicence.CorpName;
        string strPrincipal = customerLicence.Principal;
        string strFiatbound = customerLicence.Fiatbound;
        string strVirtualdate = customerLicence.Virtualdate;
        string strCorpaddr = customerLicence.Corpaddr;
        string strOrgan = customerLicence.Organ;
        string strDate = customerLicence.Date;

        customerLicence.CustomerID = intCus_ID;

        ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();

        int intExistCount = cls_Customer.CheckUpCustomerLicence(intCus_ID, strNo);

        if (intExistCount == 0)
        {
            if (strNo.Trim() != "" && strName.Trim() != "" && strVirtualdate.Trim() != "")
            {
                try
                {
                    DateTime dateTime = DateTime.Parse(strVirtualdate.Trim());

                    Boolean is_Succeed = cls_Customer.AddOrUpdateCustomerLicence(-1, intCus_ID, strNo, strName, strCorpName, strPrincipal, strFiatbound, DateTime.Parse(strVirtualdate), strCorpaddr, strOrgan, DateTime.Parse(strDate));

                    if (is_Succeed)
                    {
                        customerLicence.No = customerLicence.Name = customerLicence.CorpName = customerLicence.Principal = customerLicence.Fiatbound = customerLicence.Corpaddr = customerLicence.Organ = "";
                    }
                    else
                    {
                        this.lblJs.Text = "<script>alert('新增客户许可证信息失败，请重试！');</script>";
                    }
                }
                catch
                {
                    this.lblJs.Text = "<script>alert('有效日期格式不正确！');</script>"; 
                }
            }
        }
        else if (intExistCount > 0)
        {
            this.lblJs.Text = "<script>alert('当前客户该许可证信息已存在！');</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('新增客户许可证信息失败，请重试！');</script>";
        }
    }

    protected void customerLicence_CalcenClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerLicence customerLicence = (CustomerLicence)this.dgrdResult.Items[intIndex].FindControl("CustomerLicence");

        customerLicence.AddPanelVisible = false;
    }

    protected void customerLicence_UpdateClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        CustomerLicence customerLicence = (CustomerLicence)this.dgrdResult.Items[intIndex].FindControl("CustomerLicence");
        customerLicence.CustomerID = customerID;
        customerLicence.IsUpdate = true;
    }

    protected void customerCharter_AddClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerCharter customerCharter = (CustomerCharter)this.dgrdResult.Items[intIndex].FindControl("CustomerCharter");

        int intCus_ID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        string strNo = customerCharter.No;
        string strName = customerCharter.Name;
        string strLegal = customerCharter.Legal;
        string strEconomykind = customerCharter.Economykind;
        string strRegbankroll = customerCharter.Regbankroll;
        string strDealinbound = customerCharter.Dealinbound;
        string strDealindome = customerCharter.Dealindome;
        string strRegaddr = customerCharter.Regaddr;
        string strOrgan = customerCharter.Organ;
        string strDate = customerCharter.Date;

        customerCharter.CustomerID = intCus_ID;

        ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();

        int intExistCount = cls_Customer.CheckUpCustomerCharter(intCus_ID, strNo);

        if (intExistCount == 0)
        {
            if (strNo.Trim() != "" && strName.Trim() != "" && strDealinbound.Trim() != "")
            {
                Boolean is_Succeed = cls_Customer.AddOrUpdateCustomerCharter(-1, intCus_ID, strNo, strName, strLegal, strEconomykind, strRegbankroll, strDealinbound, strDealindome, strRegaddr, strOrgan, DateTime.Parse(strDate));

                if (is_Succeed)
                {
                    customerCharter.No = customerCharter.Name = customerCharter.Legal = customerCharter.Economykind = customerCharter.Regbankroll = customerCharter.Dealinbound = customerCharter.Dealindome = customerCharter.Regaddr = customerCharter.Organ = "";
                }
                else
                {
                    this.lblJs.Text = "<script>alert('新增客户营业执照信息失败，请重试！');</script>";
                }
            }
        }
        else if (intExistCount > 0)
        {
            this.lblJs.Text = "<script>alert('当前客户营业执照信息已存在！');</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('新增客户营业执照信息失败，请重试！');</script>";
        }
    }

    protected void customerCharter_CalcenClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerCharter customerCharter = (CustomerCharter)this.dgrdResult.Items[intIndex].FindControl("CustomerCharter");

        customerCharter.AddPanelVisible = false;
    }

    protected void customerCharter_UpdateClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        CustomerCharter customerCharter = (CustomerCharter)this.dgrdResult.Items[intIndex].FindControl("CustomerCharter");
        customerCharter.CustomerID = customerID;
        customerCharter.IsUpdate = true;
    }

    protected void cusPrice_AddClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerPrice customerPrice = (CustomerPrice)this.dgrdResult.Items[intIndex].FindControl("CustomerPrice");

        int intCus_ID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        int intCus_Price = customerPrice.PriceType;
        string strM_No = customerPrice.M_ID;
        string strRemark = customerPrice.Remark;

        customerPrice.CustomerID = intCus_ID;

        ClsCustomersInfoDAL cls_Customer = new ClsCustomersInfoDAL();

        int intExistCount = cls_Customer.CheckUpCustomerPrice(intCus_ID, strM_No);

        if (intExistCount == 0)
        {
            if (strM_No.Trim() != "")
            {
                Boolean is_Succeed = cls_Customer.AddOrUpdateCustomerPrice(-1, intCus_ID, strM_No, intCus_Price, strRemark);

                if (is_Succeed)
                {
                    customerPrice.M_ID = customerPrice.M_Name = customerPrice.Remark = "";
                }
                else
                {
                    this.lblJs.Text = "<script>alert('新增客户商品价格等级失败，请重试！');</script>";
                }
            }
        }
        else if (intExistCount > 0)
        {
            this.lblJs.Text = "<script>alert('当前客户该商品的价格等级已存在！');</script>";
        }
        else
        {
            this.lblJs.Text = "<script>alert('新增客户商品价格等级失败，请重试！');</script>";
        }
    }

    protected void cusPrice_CalcenClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        CustomerPrice customerPrice = (CustomerPrice)this.dgrdResult.Items[intIndex].FindControl("CustomerPrice");

        customerPrice.AddPanelVisible = false;
    }

    protected void cusPrice_UpdateClick(object sender, EventArgs e)
    {
        int intIndex = Int32.Parse(this.dgrdResult.ToolTip);
        int customerID = Int32.Parse(this.dgrdResult.Items[intIndex].Cells[1].Text.Trim());
        CustomerPrice customerPrice = (CustomerPrice)this.dgrdResult.Items[intIndex].FindControl("CustomerPrice");
        customerPrice.CustomerID = customerID;
        customerPrice.IsUpdate = true;
    }
}
